//
//  MsgUserVideoCell.swift
//  swiftim
//
//  Created by yangjian on 2023/6/13.
//

import Foundation

class MsgUserVideoCell : MsgUserBaseCell{
    
    let msgImageView = UIImageView()
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.configMsgSubViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configMsgSubViews() {
        configBubbleView(superView: msgBackgroundView, aboveView: msgContentView)

        msgContentView.addSubview(msgImageView)
        
        stateLabel.snp.makeConstraints { make in
            make.bottom.equalTo(self.msgContentView.snp.bottom)
            make.right.equalTo(self.msgContentView.snp.left).offset(-20)
        }
        
        msgImageView.snp.makeConstraints { make in
            make.top.equalTo(msgContentView)
            make.bottom.equalTo(msgContentView)
            make.left.equalTo(msgContentView)
            make.right.equalTo(msgContentView)
            make.size.equalTo(CGSizeMake(20, 20))
        }
        
    }
    

    override func resetSubViewsLayout(showName: Bool, showHead: Bool) {
        super.resetSubViewsLayout(showName: showName, showHead: showHead)
        resetBubbleleView(aboveView: msgContentView)
        /*
         这里说明一下,当滑动列表,触发复用时,会遇到约束冲突,
         其实是系统自动给 UITableViewCellContentView 这个添加了一个高度约束,
         而不同图片高度是变化的,从而导致复用时,重新给定高度,产生约束冲突.
         对此,我当前不知道如何解决(以前并没有遇到过这个问题,现在却有,很奇怪)
         
         不过,如果改用手动计算高度之后,估计这个问题就不再存在了.
         */
        msgImageView.snp.updateConstraints { make in
            make.size.equalTo(model.imageFitSize)
        }
    }
    
    
    override func bindFromModel(_ model: MsgCellBaseModel) {
        super.bindFromModel(model)
        
        let body = model.message.body as! EMVideoMessageBody
        guard let img = UIImage(contentsOfFile: body.thumbnailLocalPath ?? "") else {
            //显示默认图片
            return
        }
        self.msgImageView.image = img
    }
}
